package com.anno;

import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.servlet.ModelAndView;

/**
 * 处理全局异常的类
 *
 * @author lcy
 * @create 2018-01-30 14:25
 */
@ControllerAdvice
public class ExceptionHandlerAdvice {

    @ExceptionHandler(value = Exception.class)
    public ModelAndView exception(Exception ex){

        ModelAndView mav = new ModelAndView("error");
        mav.addObject("errorMessage",ex.getMessage());
        System.out.println(ex.getMessage());
        return mav;
    }

    @ModelAttribute
    public void addAttributes(Model model){
        model.addAttribute("msg","额外信息");

    }

    //@InitBinder
    //public void initBinder(WebDataBinder binder){
    //    binder.setDisallowedFields("id");
    //}
}
